package com.woniu.apartment.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.apartment.myVo.PopularApartmentVo;

import com.woniu.apartment.vo.ApartLayoutVo;
import com.woniu.apartment.vo.ApartUniRefvo;
import com.woniu.apartment.vo.ApartmentVo;
import com.woniu.apartment.domain.*;
import com.woniu.apartment.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.woniu.apartment.service.IApartmentMainService;

/**
 * 公寓Service业务层处理
 *
 * @author yy
 * @date 2023-04-03
 */
@Service
public class ApartmentMainServiceImpl implements IApartmentMainService {
    @Autowired
    private ApartmentMainMapper apartmentMainMapper;
    @Autowired
    private ApartItemsRefMapper apartItemsRefMapper;
    @Autowired
    private ApartFacilityRefMapper apartFacilityRefMapper;
    @Autowired
    private ApartImageMapper apartImageMapper;

    @Autowired
    private ApartRulesRefMapper apartRulesRefMapper;

    @Autowired
    private ApartSecurityRefMapper apartSecurityRefMapper;
    @Autowired
    private ApartUniRefMapper apartUniRefMapper;
    @Autowired
    private ApartLayoutMapper apartLayoutMapper;


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 查询公寓
     *
     * @param id 公寓主键
     * @return 公寓
     */
    @Override
    public ApartmentMain selectApartmentMainById(Long id) {
        return apartmentMainMapper.selectApartmentMainById(id);
    }

    /**
     * 查询公寓列表
     *
     * @param apartmentMain 公寓
     * @return 公寓
     */
    @Override
    public List<ApartmentMain> selectApartmentMainList(ApartmentMain apartmentMain) {
        return apartmentMainMapper.selectApartmentMainList(apartmentMain);
    }

    /**
     * 新增公寓
     *
     * @param apartmentMain 公寓
     * @return 结果
     */
    @Override
    public int insertApartmentMain(ApartmentMain apartmentMain) {
        return apartmentMainMapper.insertApartmentMain(apartmentMain);
    }

    /**
     * 修改公寓
     *
     * @param apartmentMain 公寓
     * @return 结果
     */
    @Override
    public int updateApartmentMain(ApartmentMain apartmentMain) {
        return apartmentMainMapper.updateApartmentMain(apartmentMain);
    }

    /**
     * 批量删除公寓
     *
     * @param ids 需要删除的公寓主键
     * @return 结果
     */
    @Override
    public int deleteApartmentMainByIds(Long[] ids) {
        return apartmentMainMapper.deleteApartmentMainByIds(ids);
    }

    /**
     * 删除公寓信息
     *
     * @param id 公寓主键
     * @return 结果
     */
    @Override
    public int deleteApartmentMainById(Long id) {
        return apartmentMainMapper.deleteApartmentMainById(id);
    }

    @Override
    public List<PopularApartmentVo> searchNearbyPopularCityApartmentList(String cityId) {
        //先从redis里面取值
        String json = redisTemplate.opsForValue().get("searchNearbyPopularCityApartmentList" + cityId);

        //redis里面没有数据
        if (json == null) {
            //加锁
            synchronized (ApartmentMainServiceImpl.class) {
                //在查询一次
                String jsonAgain = redisTemplate.opsForValue().get("searchNearbyPopularCityApartmentList");

                //redis里面没有数据的话

                List<PopularApartmentVo> list = apartmentMainMapper.searchNearbyPopularCityApartmentList(cityId);
                String s = null;
                try {
                    s = objectMapper.writeValueAsString(list);
                    redisTemplate.opsForValue().set("searchNearbyPopularCityApartmentList" + cityId, s);
                    return list;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }


            }


        }
        //redis里面有数据的话

        try {
           /* List<PopularApartmentVo> list = objectMapper.readValue(json, new TypeReference<List<PopularApartmentVo>>() {
            });*/
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, PopularApartmentVo.class);
            ArrayList<PopularApartmentVo> list = objectMapper.readValue(json, javaType);
            /*ArrayList<PopularApartmentVo> a = objectMapper.readValue(json, ArrayList.class);*/

            return list;

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        /*return apartmentMainMapper.searchNearbyPopularCityApartmentList(cityId);*/
        return null;
    }

    @Override
    public List<PopularApartmentVo> SearchCityOrUniversityOrApartmentNameForList(String searchContent) {
        //先判断searchContent是不是英文名
        String s = detectLanguage(searchContent);
        List<PopularApartmentVo> list = new ArrayList<>();
        if ("中文".equals(s)){
            System.out.println("中文:"+searchContent);
            //第一步先查询城市
            list = apartmentMainMapper.searchApartmentListByCityName(searchContent);
            System.out.println("第一步先查询城市===="+list);
            if (list.size() != 0) {
                Iterator<PopularApartmentVo> iterator = list.iterator();
                while (iterator.hasNext()){
                    iterator.next().doRentInclusion();
                }
                System.out.println(list);
                return list;
            }

            //第二步查询大学
            list = apartmentMainMapper.searchApartmentListByUniversityName(searchContent);
            System.out.println("第二步先查询大学===="+list);
            if (list.size() != 0) {
                Iterator<PopularApartmentVo> iterator = list.iterator();
                while (iterator.hasNext()){
                    iterator.next().doRentInclusion();
                }
                System.out.println(list);
                return list;
            }



        }else {
            System.out.println("英文:"+searchContent);

            //第一步先查询英文名字城市
            list = apartmentMainMapper.searchApartmentListByECityName(searchContent);
            System.out.println("第一步先查询英文城市名===="+list);
            if (list.size() != 0) {
                Iterator<PopularApartmentVo> iterator = list.iterator();
                while (iterator.hasNext()){
                    iterator.next().doRentInclusion();
                }
                System.out.println(list);
                return list;
            }

            //第二步查询英文名字大学
            list = apartmentMainMapper.searchApartmentListByEUniversityName(searchContent);
            System.out.println("第二步先查询英文大学名===="+list);
            if (list.size() != 0) {
                Iterator<PopularApartmentVo> iterator = list.iterator();
                while (iterator.hasNext()){
                    iterator.next().doRentInclusion();
                }
                System.out.println(list);
                return list;
            }



            //查询公寓名(公寓只有英文名)
            list = apartmentMainMapper.searchApartmentListByApartmentName(searchContent);
            System.out.println("第三步先查询公寓名(英文)===="+list);
            if (list.size() != 0) {
                Iterator<PopularApartmentVo> iterator = list.iterator();
                while (iterator.hasNext()){
                    iterator.next().doRentInclusion();
                }
                System.out.println(list);
                return list;
            }


        }


        return list;
    }



    //判断是否为中文的方法
    public static String detectLanguage(String str) {
        boolean isChinese = false;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            // 如果字符的Unicode编码在中文范围内，则认为是中文
            if ((c >= 0x4e00) && (c <= 0x9fbb)) {
                isChinese = true;
                break;
            }
        }
        return isChinese ? "中文" : "英文";
    }


    /*公寓详情页面所有消息*/
    @Override
    public ApartmentVo selectAllInfoById(Long id) {
        //------------------------全部信息
        ApartmentVo apartmentVo = new ApartmentVo();
        //------------------------公寓信息
        ApartmentMain apartmentMain = apartmentMainMapper.selectPartInfoById(id);
        //------------------------费用包括集合
        List<Items> apartItemsRefList = apartItemsRefMapper.selectByID(id);
        //------------------------费用包括,字符串
        String s = apartItemsRefList.toString();
        System.out.println("======================================"+s);
        //------------------------设施集合
        List<Facility> apartFacilityRef = apartFacilityRefMapper.selectByID(id);
        //------------------------公寓小照片
        List<ApartImage> apartImages = apartImageMapper.selectByID(id);
        //------------------------公寓距离Vo
        List<ApartUniRefvo> apartUniRefvos = apartUniRefMapper.selectByID(id);
        //------------------------公寓规则
        List<Rules> apartRulesRef = apartRulesRefMapper.selectByID(id);
        //------------------------公寓安保
        List<Security> ApartSecurityRef = apartSecurityRefMapper.selectByID(id);
        //------------------------公寓放行Vo
        List<ApartLayoutVo> apartLayoutVos = apartLayoutMapper.selectByID(id);

        return creatApartment(id, apartmentVo, apartmentMain, apartItemsRefList, s, apartFacilityRef, apartImages, apartUniRefvos, apartRulesRef, ApartSecurityRef, apartLayoutVos);

    }

    private static ApartmentVo creatApartment(Long id, ApartmentVo apartmentVo, ApartmentMain apartmentMain, List<Items> apartItemsRefList, String s, List<Facility> apartFacilityRef, List<ApartImage> apartImages, List<ApartUniRefvo> apartUniRefvos, List<Rules> apartRulesRef, List<Security> ApartSecurityRef, List<ApartLayoutVo> apartLayoutVos) {
        apartmentVo.setId(id);
        apartmentVo.setApartmentName(apartmentMain.getApartmentName());
        apartmentVo.setImageUrl(apartmentMain.getImageUrl());
        apartmentVo.setCurrency(apartmentMain.getCurrency());
        apartmentVo.setComments(apartmentMain.getComments());
        apartmentVo.setRent(apartmentMain.getRent());
        apartmentVo.setCurrency(apartmentMain.getCurrency());
        apartmentVo.setRentInclusion(s);
        apartmentVo.setAddressInfo(apartmentMain.getAddressInfo());
        apartmentVo.setBriefDesc(apartmentMain.getBriefDesc());
        apartmentVo.setFacilityScore(apartmentMain.getFacilityScore());
        apartmentVo.setTrafficScore(apartmentMain.getTrafficScore());
        apartmentVo.setSecurityScore(apartmentMain.getSecurityScore());
        apartmentVo.setServiceScore(apartmentMain.getServiceScore());
        apartmentVo.setCostPerScore(apartmentMain.getCostPerScore());
        apartmentVo.setScore(apartmentMain.getScore());
        apartmentVo.setRentIncludsion(apartItemsRefList);
        apartmentVo.setFacilityList(apartFacilityRef);
        apartmentVo.setApartImageList(apartImages);
        apartmentVo.setUniversityAroundApartList(apartUniRefvos);
        apartmentVo.setRuleList(apartRulesRef);
        apartmentVo.setSecurityList(ApartSecurityRef);
        apartmentVo.setApartLayoutList(apartLayoutVos);

        return apartmentVo;
    }
}
